﻿using System;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace animexx.OAuth
{
        /// <summary>
        /// Provides an internal structure to sort the query parameter
        /// </summary>
        public class QueryParameter
        {
            private string name = null;
            private string value = null;

            public QueryParameter(string name, string value)
            {
                this.name = name;
                this.value = value;
            }

            public string Name
            {
                get { return name; }
            }

            public string Value
            {
                get { return value; }
            }

            /// <summary>
            /// Internal function to cut out all non oauth query string parameters (all parameters not begining with "oauth_")
            /// </summary>
            /// <param name="parameters">The query string part of the Url</param>
            /// <returns>A list of QueryParameter each containing the parameter name and value</returns>
            public static List<QueryParameter> GetQueryParameters(string parameters)
            {
                if (parameters.StartsWith("?"))
                {
                    parameters = parameters.Remove(0, 1);
                }

                List<QueryParameter> result = new List<QueryParameter>();

                if (!string.IsNullOrEmpty(parameters))
                {
                    string[] p = parameters.Split('&');
                    foreach (string s in p)
                    {
                        if (!string.IsNullOrEmpty(s))
                        {
                            if (s.IndexOf('=') > -1)
                            {
                                string[] temp = s.Split('=');
                                result.Add(new QueryParameter(temp[0], temp[1]));
                            }
                            else
                            {
                                result.Add(new QueryParameter(s, string.Empty));
                            }
                        }
                    }
                }
                return result;
            }
        }

        /// <summary>
        /// Comparer class used to perform the sorting of the query parameters
        /// </summary>
        public class QueryParameterComparer : IComparer<QueryParameter>
        {

            #region IComparer<QueryParameter> Members

            public int Compare(QueryParameter x, QueryParameter y)
            {
                if (x.Name == y.Name)
                {
                    return string.Compare(x.Value, y.Value);
                }
                else
                {
                    return string.Compare(x.Name, y.Name);
                }
            }

            #endregion
        }

        
}
